package org.tea.inventory.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tea.inventory.dao.tea.ProductMapper;
import org.tea.inventory.dao.tea.WarehouseInfoMapper;
import org.tea.inventory.dao.tea.WarehouseInventoryMapper;
import org.tea.inventory.domain.tea.*;
import org.tea.inventory.service.IWarehouseInfoService;
import org.tea.inventory.utils.UserUtil;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 仓库信息Service业务层处理
 * 
 * @author dongxb
 * @date 2025-04-14
 */
@Service
public class WarehouseInfoServiceImpl implements IWarehouseInfoService
{
    @Autowired
    private WarehouseInfoMapper warehouseInfoMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private WarehouseInventoryMapper warehouseInventoryMapper;

    /**
     * 查询仓库信息
     * 
     * @param id 仓库信息主键
     * @return 仓库信息
     */
    @Override
    public WarehouseInfo selectWarehouseInfoById(Long id)
    {
        return warehouseInfoMapper.selectWarehouseInfoById(id);
    }

    /**
     * 查询仓库信息列表
     * 
     * @param warehouseInfo 仓库信息
     * @return 仓库信息
     */
    @Override
    public List<WarehouseInfo> selectWarehouseInfoList(WarehouseInfo warehouseInfo)
    {
        warehouseInfo.setDelFlag(0);
        return warehouseInfoMapper.selectWarehouseInfoList(warehouseInfo);
    }

    /**
     * 新增仓库信息
     * 
     * @param warehouseInfo 仓库信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertWarehouseInfo(WarehouseInfo warehouseInfo)
    {
        warehouseInfo.setCreateDate(new Date());
        warehouseInfo.setCreateUserId(UserUtil.getUser().getLoginId());
        int a=warehouseInfoMapper.insertWarehouseInfo(warehouseInfo);
        if(a>0){
            //查询所有商品ID
            List<Long> productIds = productMapper.findAllProductIds();
            //准备批量插入数据
            List<WarehouseInventory> relations = productIds.stream()
                    .map(productId -> new WarehouseInventory(
                            warehouseInfo.getId(),
                            productId,
                            0L,
                            new Date()
                    )).collect(Collectors.toList());
            // 批量插入仓库库存基础信息
            if(!relations.isEmpty()) {
                warehouseInventoryMapper.batchInsertWarehouseInventory(relations);
            }
        }
        return a;
    }

    /**
     * 修改仓库信息
     * 
     * @param warehouseInfo 仓库信息
     * @return 结果
     */
    @Override
    public int updateWarehouseInfo(WarehouseInfo warehouseInfo)
    {
        warehouseInfo.setUpdateDate(new Date());
        warehouseInfo.setUpdateUserId(UserUtil.getUser().getLoginId());
        return warehouseInfoMapper.updateWarehouseInfo(warehouseInfo);
    }

    /**
     * 批量删除仓库信息
     * 
     * @param ids 需要删除的仓库信息主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseInfoByIds(Long[] ids)
    {
        return warehouseInfoMapper.deleteWarehouseInfoByIds(ids);
    }

    /**
     * 删除仓库信息信息
     * 
     * @param id 仓库信息主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseInfoById(Long id)
    {
        WarehouseInfo warehouseInfo = new WarehouseInfo();
        warehouseInfo.setId(id);
        warehouseInfo.setDelFlag(1);
        return warehouseInfoMapper.updateWarehouseInfo(warehouseInfo);
    }
}
